import 'package:flutter/material.dart';
import 'package:x3_base_core/base_models/base_key_value_model.dart';
import 'package:x3_form_core/filter_components/mixin/advanced_filter_select_mixin.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';

/// author：william on 2021/4/12 14:58
/// email：362661044@qq.com
class AdvancedFilterMultiChoice extends StatefulWidget {
  final BaseAdvancedFilterModel model;

  const AdvancedFilterMultiChoice({Key key, @required this.model}) : super(key: key);

  @override
  _AdvancedFilterMultiChoiceState createState() => _AdvancedFilterMultiChoiceState();
}

class _AdvancedFilterMultiChoiceState extends State<AdvancedFilterMultiChoice> with AdvancedFilterSelectMixin<AdvancedFilterMultiChoice> {
  @override
  Widget build(BuildContext context) {
    return buildItemContainer();
  }

  @override
  List<BaseKeyValueModel> get children => widget.model.selectData ?? [];

  @override
  BaseAdvancedFilterModel get model => widget.model;

  @override
  void onChange(bool v, BaseKeyValueModel data) {
    /// 如果是勾选全部的话
    if (data.key.toString() == allKey.toString() && v == true) {
      selectedList.clear();
    } else {
      bool isAll = selectedListKey.contains(allKey.toString());
      if (isAll == true) selectedList.removeWhere((element) => element.key.toString() == allKey.toString());
    }
    onChangeEvent(v, data);
    baseRequestFormProvider.setDataSource(this.model, selectedList.map((e) => e.toJson()).toList());
    setState(() {});
  }
}
